<?php

// error_reporting(E_ALL);

if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "ModTest::main");
}

require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";

/* dependencies for EE */
global $REGX, $PREFS, $DB, $GBL, $IN, $EXT, $FNS, $LOC, $LANG, $SESS, $DSP, $OUT, $BM;

include_once "systempath.php";
print("SYSTEM PATH: $system_path\n\n");//TMP

if(empty($system_path)) // couldn't read the include file
{
	if (eregi("win", PHP_OS)) 
	{
		$system_path = "c:/NCEIntranet/build/Intranet/system/";    
	}
	else
	{
		$system_path = "/var/www/ee/intranet_webdev/system/";
	}
}

$tmp = $system_path;

$ext = ".php";
define("EXT", $ext); // needed for the module class to load

require_once $system_path."config.php";

ob_start();
require_once $system_path."core/core.system.php";
ob_end_clean();

$DB = new DB($db_config);
$LANG->fetch_language_file('ncb_rooms');

$system_path = $tmp;

require_once $system_path."modules/weblog/mod.weblog.php";
require_once "mod.ncb_rooms.php";

/**
 * Test class for module
 */
class ModTest extends PHPUnit_Framework_TestCase 
{

	var $module_obj = null;

	public static function main()
	{
		require_once "PHPUnit2/TextUI/TestRunner.php";
		$suite  = new PHPUnit_Framework_TestSuite("ModTest");
		$result = PHPUnit_TextUI_TestRunner::run($suite);
	}


	protected function setUp()
	{
		$this->module_obj = new ncb_rooms();
	}

	protected function tearDown()
	{
		$this->module_obj = null;
	}
	
	/************ tests for is_requested_booking_time_past **************/
	
	/**
	 * Date in the past should return TRUE
	 */ 
	function test_is_requested_booking_time_past_1()
	{
		$now = time();
		$hour = date("H", $now); $min = date("i", $now); $sec = date("s", $now);
		$month = date("m", $now); $day = date("d", $now); $year = date("Y", $now);

		$unix_booking_time = mktime($hour - 1, $min, $sec, $month, $day, $year); // one hour in the past
		$expected = TRUE;
		$actual = $this->module_obj->is_requested_booking_time_past($unix_booking_time);
	
		$this->assertEquals($expected, $actual, "The date $day/$month/$year $hour:$minute:$second was supposed to be in the past");
	}

	/**
	 * Date in the past should return TRUE
	 */ 
	function test_is_requested_booking_time_past_2()
	{
		$now = time();
		$hour = date("H", $now); $min = date("i", $now); $sec = date("s", $now);
		$month = date("m", $now); $day = date("d", $now); $year = date("Y", $now);

		$unix_booking_time = mktime($hour + 1, $min, $sec, $month, $day, $year - 1); // one year - one hour in the past
		$expected = TRUE;
		$actual = $this->module_obj->is_requested_booking_time_past($unix_booking_time);
	
		$this->assertEquals($expected, $actual, "The date $day/$month/$year $hour:$minute:$second was supposed to be in the past");
	}

	
	/**
	 * Date in the future should return FALSE
	 */ 
	function test_is_requested_booking_time_past_3()
	{
		$now = time();
		$hour = date("H", $now); $min = date("i", $now); $sec = date("s", $now);
		$month = date("m", $now); $day = date("d", $now); $year = date("Y", $now);

		$unix_booking_time = mktime($hour + 1, $min, $sec, $month, $day, $year); // one hour in the future
		$expected = FALSE;
		$actual = $this->module_obj->is_requested_booking_time_past($unix_booking_time);
	
		$this->assertEquals($expected, $actual, "The date $day/$month/$year $hour:$minute:$second was supposed to be in the future");
	}


	/**
	 * Date in the future should return FALSE
	 */ 
	function test_is_requested_booking_time_past_4()
	{
		$now = time();
		$hour = date("H", $now); $min = date("i", $now); $sec = date("s", $now);
		$month = date("m", $now); $day = date("d", $now); $year = date("Y", $now);

		$unix_booking_time = mktime($hour - 1, $min, $sec, $month, $day, $year + 1); // one year - one hour in the future
		$expected = FALSE;
		$actual = $this->module_obj->is_requested_booking_time_past($unix_booking_time);
	
		$this->assertEquals($expected, $actual, "The date $day/$month/$year $hour:$minute:$second was supposed to be in the future");
	}

	/************ tests for is_requested_meeting_within_booking_day	 **************/

	/**
	 * Test a meeting within the current booking day - should return TRUE
	 */ 
	function test_is_requested_meeting_within_booking_day_1()
	{
		$this->module_obj->booking_date = "2007-03-20";
		$this->module_obj->start_time = "08:00";
		$this->module_obj->duration_minutes = 8 * 60;
		$this->module_obj->earliest_slot_hour = 8;
		$this->module_obj->latest_slot_hour = 20;
		
		$expected = TRUE;
		$actual = $this->module_obj->is_requested_meeting_within_booking_day();
	
		$this->assertEquals($expected, $actual, "The meeting at ".$this->module_obj->start_time." (".($this->module_obj->duration_minutes / 60)." hours) was supposed to be within the booking day (".$this->module_obj->earliest_slot_hour.":00 - ".$this->module_obj->latest_slot_hour.":00)");
	}
	
	/**
	 * Test a meeting that exceeds the current booking day - should return FALSE
	 */ 
	function test_is_requested_meeting_within_booking_day_2()
	{
		$this->module_obj->booking_date = "2007-03-20";
		$this->module_obj->start_time = "14:00";
		$this->module_obj->duration_minutes = 8 * 60;
		$this->module_obj->earliest_slot_hour = 8;
		$this->module_obj->latest_slot_hour = 20;
		
		$expected = FALSE;
		$actual = $this->module_obj->is_requested_meeting_within_booking_day();
	
		$this->assertEquals($expected, $actual, "The meeting at ".$this->module_obj->start_time." (".($this->module_obj->duration_minutes / 60)." hours) was supposed to exceed the booking day (".$this->module_obj->earliest_slot_hour.":00 - ".$this->module_obj->latest_slot_hour.":00)");
	}

	/************ tests for is_booking_in_current_slot	 **************/

	/**
	 * Test a booking to make sure it is within the current slot time - should return TRUE
	 */ 
	function test_is_booking_in_current_slot_1()
	{
		$this->module_obj->settings['booking_granularity'] = 15;

		$now = time();
//		$hour = date("H", $now); $min = date("i", $now); $sec = date("s", $now);
//		$month = date("m", $now); $day = date("d", $now); $year = date("Y", $now);

//		$unix_booking_time = mktime($hour, $min + 5, $sec, $month, $day, $year); // 5 minutes in the future

		$unix_booking_time = $now;

		$hour = date("H", $unix_booking_time); $min = date("i", $unix_booking_time); $sec = date("s", $unix_booking_time);
		$month = date("m", $unix_booking_time); $day = date("d", $unix_booking_time); $year = date("Y", $unix_booking_time);
		
		$times = $this->module_obj->get_current_slot_times();
		$keys = array_keys($times);
		$start = $keys[0];
		$end = $times[$start];
		$disp_start = date("d/m/Y H:i:s", $start);
		$disp_end = date("d/m/Y H:i:s", $end);

		$expected = TRUE;
		$actual = $this->module_obj->is_booking_in_current_slot($unix_booking_time);
					
		$this->assertEquals($expected, $actual, "\nThe booking at $hour:$min:$sec on $day/$month/$year ($unix_booking_time)\nwas supposed to be within the current booking slot:\n[$disp_start ($start) - $disp_end ($end)]");
	}
	
	/**
	 * Test a booking to make sure it is within the current slot time - should return FALSE
	 */ 
	function test_is_booking_in_current_slot_2()
	{
		$this->module_obj->settings['booking_granularity'] = 15;

		$now = time();
		$hour = date("H", $now); $min = date("i", $now); $sec = date("s", $now);
		$month = date("m", $now); $day = date("d", $now); $year = date("Y", $now);

		$unix_booking_time = mktime($hour - 1, $min, $sec, $month, $day, $year); // one hour in the past

		$hour = date("H", $unix_booking_time); $min = date("i", $unix_booking_time); $sec = date("s", $unix_booking_time);
		$month = date("m", $unix_booking_time); $day = date("d", $unix_booking_time); $year = date("Y", $unix_booking_time);
		
		$times = $this->module_obj->get_current_slot_times();
		$keys = array_keys($times);
		$start = $keys[0];
		$end = $times[$start];
		$disp_start = date("d/m/Y H:i:s", $start);
		$disp_end = date("d/m/Y H:i:s", $end);

		$expected = FALSE;
		$actual = $this->module_obj->is_booking_in_current_slot($unix_booking_time);
			
		$this->assertEquals($expected, $actual, "\nThe booking at $hour:$min:$sec on $day/$month/$year ($unix_booking_time)\nwas supposed to not be within the current booking slot current booking slot:\n[$disp_start ($start) - $disp_end ($end)]");
	}

	/************ tests for get_booking_as_unixtime	 **************/
	
	/**
	 *
	 **/
	function test_get_booking_as_unixtime_1()
	{
		$booking_date = "2007-03-20";
		$start_time = "08:15";
		
		$expected = mktime(8, 15, 59, 3, 20, 2007);
		$actual = $this->module_obj->get_booking_as_unixtime($booking_date, $start_time);
		
		$this->assertEquals($expected, $actual, "The timestamp for the booking at $start_time on $booking_date was supposed to be: $expected instead it was $actual (".date("Y-m-d H:i:s", $actual).")");		
	}
	

} // end class ModTest


// Call main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "ModTest::main") {
    ModTest::main();
}
?>
